/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-11
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description: 
 */
import 'package:chat_client/providers/login_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

// 自定义组件库
import 'package:chat_client/widgets/captcha.dart';
import 'package:chat_client/widgets/c_input.dart';
import 'package:chat_client/widgets/c_square_avatar.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});

  @override
  State<RegisterPage> createState() => _RegisterScreenState();
}

class _RegisterScreenState extends State<RegisterPage> {
  @override
  Widget build(BuildContext context) {
    // 更具屏幕宽度设置padding
    double padding = MediaQuery.of(context).size.width * 0.15;

    return Scaffold(
      appBar: AppBar(
        title: Text('账号注册'),
        backgroundColor: Colors.white,
      ),
      body: Padding(
        padding: EdgeInsets.all(padding),
        child: ChangeNotifierProvider(
          create: (_) => LoginProvider(),
          child: RegisterForm(),
        ),
      ),
    );
  }
}

class RegisterForm extends StatelessWidget {
  const RegisterForm({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<LoginProvider>(builder: (context, model, child) {
      return ListView(
        children: [
          SizedBox(height: 20),
          Center(
            child: GestureDetector(
              onTap: () {
                model.pickImage();
              },
              child: SquareAvatar(
                imageSource: model.imageXFile,
                size: 100,
                borderRadius: 0,
                icon: Icons.camera_alt,
                backgroundColor: const Color(0XFFEEEEEE),
              ),
            ),
          ),
          // 用户昵称
          InputField(
            controller: model.nicknameController,
            icon: Icons.person,
            hintText: '用户昵称',
            simplified: true,
          ),
          InputField(
            controller: model.phoneController,
            maxLength: 11,
            icon: Icons.phone_android_outlined,
            hintText: '电话号码',
            simplified: true,
          ),
          InputField(
            controller: model.captchaController,
            maxLength: 6,
            icon: Icons.safety_check,
            hintText: '验证码',
            rightSlot:
                Captcha(onCaptchaUidChanged: model.handleCaptchaUidChanged),
            simplified: true,
          ),
          InputField(
            controller: model.passwordController,
            icon: Icons.lock,
            hintText: '密码',
            isPassword: true,
            simplified: true,
          ),
          const SizedBox(height: 40),
          ElevatedButton(
            onPressed: () {
              model.register();
            },
            child: const Text('注册'),
          ),
        ],
      );
    });
  }
}
